package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.activity.CommentActivity.MyAdapter;
import com.ycsj.goldmedalnewconcept.bean.SumCommentResp;
import com.ycsj.goldmedalnewconcept.bean.SumCommentResp.SumCommentlistDetail;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class AddCommentActivity extends Activity implements OnClickListener{

	private Button bt_content;
	private ImageView iv_back;
	private ImageView iv_addcomment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addcomment);
		type = getIntent().getStringExtra("type");
		rowid = getIntent().getStringExtra("rowid");
		initView();
		setOnListener();
		initData();
	}

	private void initView() {
		iv_back = (ImageView) findViewById(R.id.iv_back);
		bt_content = (Button) findViewById(R.id.bt_content);
		 et_content = (EditText) findViewById(R.id.et_content);
		ratingBar = (RatingBar) findViewById(R.id.ratingBar); 
		
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_sex = (TextView) findViewById(R.id.tv_sex);
		tv_tel = (TextView) findViewById(R.id.tv_tel);
		iv_addcomment = (ImageView) findViewById(R.id.iv_addcomment);
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
		bt_content.setOnClickListener(this);
	}

	private void initData() {
		text = et_content.getText().toString().trim();
		rating = ratingBar.getRating();
		
//		OkHttpClient mOkHttpClient = new OkHttpClient();
//		// 创建一个Request
//		final Request request = new Request.Builder()
//				.url("http://192.168.0.198:8888/ycsj_platform/PersonalAssistantMsg?type=13&c=15098012693&role=1")
//				.build();
//		// new call13355556665
//		Call call = mOkHttpClient.newCall(request);
//		call.enqueue(callBack2);
		
		OkHttpClient mOkHttpClient = new OkHttpClient();
//		mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "13");
		builder.add("c", SPUtil.getString(AddCommentActivity.this, "account", ""));
		builder.add("role", SPUtil.getString(AddCommentActivity.this, "role", ""));
		Request request = new Request.Builder()
		.url(Constant.SERVER_URL + "PersonalAssistantMsg")
		.post(builder.build()).build();
		mOkHttpClient.newCall(request).enqueue(callBack2);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		case R.id.bt_content:
			text = et_content.getText().toString();
			rating = ratingBar.getRating();
			
			Log.e("test", "text="+text);
			Log.e("test", "rating="+rating);
			if(text!=null&&(!"".equals(text))&&rating!=0.0){
				if(type.equals("8")){
//					OkHttpClient mOkHttpClient = new OkHttpClient();
//					// 创建一个Request
//					final Request request = new Request.Builder()
//					.url("http://192.168.0.198:8888/ycsj_platform/PersonalAssistantMsg?type=8&c=15333838051&role=3&user_class=1&teachermobile=15333838051&score=92&content="+text+"&status=0")
//					.build();
//					// new call
//					Call call = mOkHttpClient.newCall(request);
//					call.enqueue(callBack);
					
					OkHttpClient mOkHttpClient = new OkHttpClient();
//					mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
					FormEncodingBuilder builder = new FormEncodingBuilder();
					builder.add("type", "8");
					builder.add("c", SPUtil.getString(AddCommentActivity.this, "account", ""));
					builder.add("role", SPUtil.getString(AddCommentActivity.this, "role", ""));
					builder.add("user_class", "1");
					builder.add("teachermobile", tel);
					builder.add("score", ""+(int)rating);
					builder.add("content", text);
					builder.add("status", "0");
					Request request = new Request.Builder()
					.url(Constant.SERVER_URL + "PersonalAssistantMsg")
					.post(builder.build()).build();
					mOkHttpClient.newCall(request).enqueue(callBack);
					
				}else if(type.equals("9")){
					//修改
//					OkHttpClient mOkHttpClient = new OkHttpClient();
//					// 创建一个Request
//					final Request request = new Request.Builder()
//					.url("http://192.168.0.198:8888/ycsj_platform/PersonalAssistantMsg?type=9&rowid="+rowid+"&c=18672626702&role=3&user_class=1&teachermobile=13311112222&score=92&content="+text+"&status=0")
//					.build();
//					// new call
//					Call call = mOkHttpClient.newCall(request);
//					call.enqueue(callBack);
					
					
					OkHttpClient mOkHttpClient = new OkHttpClient();
//					mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
					FormEncodingBuilder builder = new FormEncodingBuilder();
					builder.add("type", "9");
					builder.add("rowid", rowid);
					builder.add("c", SPUtil.getString(AddCommentActivity.this, "account", ""));
					builder.add("role", SPUtil.getString(AddCommentActivity.this, "role", ""));
					builder.add("user_class", "1");
					builder.add("teachermobile", tel);
					builder.add("score", ""+(int)rating);
					builder.add("content", text.toString());
					builder.add("status", "0");
					Request request = new Request.Builder()
					.url(Constant.SERVER_URL + "PersonalAssistantMsg")
					.post(builder.build()).build();
					mOkHttpClient.newCall(request).enqueue(callBack);
					
				}
			}else{
				Toast.makeText(AddCommentActivity.this, "您尚未给老师打分或评语为空", 0).show();
			}
			
			break;

		default:
			break;
		}
	}
	 
	Callback callBack=new Callback() {
		
		@Override
		public void onResponse(Response arg0) throws IOException {
			Log.e("testtips", arg0.body().string());
			if(type.equals("8")){
				handler.postDelayed(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						
						Toast.makeText(AddCommentActivity.this, "提交成功", 0).show();
						
					}
				}, 10);
			}else{
handler.postDelayed(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						
						Toast.makeText(AddCommentActivity.this, "修改成功", 0).show();
						
					}
				}, 10);
				
			}
		
			Intent intent=new Intent(AddCommentActivity.this, ParentsEvaluationActivity.class);
			startActivity(intent);
			
		}
		
		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub
			
		}
	};
	public String tel;
	private EditText et_content;
	private RatingBar ratingBar;
	private String text;
	private float rating;
	private String type;
	private String rowid;
	private List<SumCommentlistDetail> list = new ArrayList<SumCommentlistDetail>();
	Handler handler = new Handler();
	Callback callBack2 = new Callback() {

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onResponse(Response arg0) throws IOException {
			String json = arg0.body().string();
			Log.e("test", json);
			Gson gson = new Gson();
			list.removeAll(list);
			if(!("{\"state\":-1}").equals(json)){
			final SumCommentResp fromJson = gson.fromJson(json, SumCommentResp.class);
			if(!fromJson.list.isEmpty()){
				list.addAll(fromJson.list);
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {
						tv_name.setText("姓名:"+fromJson.list.get(0).NAME);
						if("1".equals(fromJson.list.get(0).NAME)){
							
							tv_sex.setText("性别:男♂");
						}else{
							tv_sex.setText("性别:女♀");
						}
						 tel=fromJson.list.get(0).TEACHER_MOBILE;
						tv_tel.setText("电话:"+tel);
						Picasso.with(AddCommentActivity.this).load(fromJson.list.get(0).ICON).into(iv_addcomment);
					}
				}, 0);
			}
		}
		}

	};
	private TextView tv_name;
	private TextView tv_sex;
	private TextView tv_tel;
	
	
}
